package com.rede.didiok.user.web;

import java.util.List;
import java.util.stream.Collectors;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.rede.didiok.common.annotion.AvoidRepeatableCommit.AvoidRepeatableCommit;
import com.rede.didiok.common.dto.community.MomentCommentDto;
import com.rede.didiok.common.dto.pay.PayOrderDto;
import com.rede.didiok.common.dto.rank.MemberCommentDto;
import com.rede.didiok.common.dto.user.NoticeDto;
import com.rede.didiok.common.exception.ThrowableUtils;
import com.rede.didiok.common.holder.RequestHolder;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.validator.group.Delete;
import com.rede.didiok.common.validator.group.GetList;
import com.rede.didiok.common.validator.group.Insert;
import com.rede.didiok.common.validator.group.Update;
import com.rede.didiok.user.entity.NoticeEntity;
import com.rede.didiok.user.service.NoticeService;

// import org.apache.shiro.authz.annotation.RequiresPermissions;



/**
 * 通知表
 *
 * @author Rede
 * @email didiok@foxmail.com
 * @date 2023-08-07 18:21:17
 */
@Slf4j
@RestController
@RequestMapping("user/feign/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @ApiOperation(value = "获取通知列表", notes = "获取通知列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody NoticeDto noticeDto, BindingResult result) {

        ThrowableUtils.checkParamArgument(result);
        log.info("获取通知列表");
        return ResultUtil.successWithData(noticeService.getPageList(noticeDto));
    }

    @AvoidRepeatableCommit
    @ApiOperation(value = "增加通知", notes = "增加通知", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody NoticeDto noticeDto, BindingResult result) {

        ThrowableUtils.checkParamArgument(result);
        return noticeService.addNotice(noticeDto);
    }

    /**
     * 保存notice
     * @param notice
     * @return
     */
    @PostMapping(value = "/user/feign/notice/saveNotice", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = "application/json;charset=UTF-8")
    String saveNotice(@RequestBody NoticeDto notice){
        NoticeEntity noticeEntity = new NoticeEntity();
        BeanUtils.copyProperties(notice, noticeEntity);
        return ResultUtil.successWithData(noticeEntity.insert());
    }

    @ApiOperation(value = "编辑通知", notes = "编辑通知", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody NoticeDto noticeDto, BindingResult result) {

        ThrowableUtils.checkParamArgument(result);
        return noticeService.editNotice(noticeDto);
    }

    @ApiOperation(value = "删除通知", notes = "删除通知", response = String.class)
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody NoticeDto noticeDto, BindingResult result) {

        ThrowableUtils.checkParamArgument(result);
        return noticeService.deleteNotice(noticeDto);
    }

    @ApiOperation(value = "获取后台站内信列表", notes = "获取后台站内信列表", response = String.class)
    @PostMapping("/getBackNoticeList")
    public String getBackNoticeList(@RequestBody NoticeDto noticeDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        return ResultUtil.successWithData(noticeService.getBackNoticeList(noticeDto));
    }

    @ApiOperation(value = "批量删除后台通知", notes = "批量删除后台通知", response = String.class)
    @PostMapping("/deleteBatchBlackNotice")
    public String deleteBatchBlackNotice(@Validated({Delete.class}) @RequestBody List<NoticeDto> noticeDtoList, BindingResult result) {

        ThrowableUtils.checkParamArgument(result);
        return noticeService.deleteBatchNotice(noticeDtoList);
    }

    @ApiOperation(value = "删除站内信小红点提示", notes = "删除站内信小红点提示", response = String.class)
    @PostMapping("/deleteRedisBatchNotice")
    public void deleteRedisBatchNotice() {
        String adminUid = RequestHolder.getAdminUid();
        noticeService.deleteRedisBatchNotice(adminUid);
    }

    @ApiOperation(value = "获取后台站内信消息数量", notes = "获取后台站内信消息数量", response = String.class)
    @PostMapping("/searchAllNoticeCount")
    public String searchAllNoticeCount() {
        String adminUid = RequestHolder.getAdminUid();
        return ResultUtil.successWithData(noticeService.searchAllNoticeCount(adminUid));
    }

    @ApiOperation(value = "通过榜单成员评论发送通知", notes = "通过榜单成员评论发送通知", response = String.class)
    @PostMapping("/sendNoticeByMemberComment")
    public String sendNoticeByMemberComment(@RequestBody MemberCommentDto comment) {
        return ResultUtil.successWithData(noticeService.sendNoticeByMemberComment(comment));
    }

    /**
     * 通过用户动态评论发送通知
     * @param comment
     * @return
     */
    @PostMapping("/sendNoticeByMomentComment")
    String sendNoticeByMomentComment(@RequestBody MomentCommentDto comment){
        return ResultUtil.successWithData(noticeService.sendNoticeByMomentComment(comment));
    }

    /**
     * 通过用户动态评论发送通知
     * @param payOrder
     * @return
     */
    @PostMapping(value = "/sendNoticeByPayOrder")
    String sendNoticeByPayOrder(@RequestBody PayOrderDto payOrder){
        return ResultUtil.successWithData(noticeService.sendNoticeByPayOrder(payOrder));
    }

    /**
     * 通过用户动态评论发送通知
     * @param payOrder
     * @return
     */
    @PostMapping(value = "/sendNoticeByCancelOrder")
    String sendNoticeByCancelOrder(@RequestBody PayOrderDto payOrder){
        return ResultUtil.successWithData(noticeService.sendNoticeByCancelOrder(payOrder));
    }

    /**
     * 批量向关注该用户【管理员】的人发送通知
     *
     * @param noticeVO
     * @return
     */
    @PostMapping(value = "/user/feign/notice/watch/add/batch", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = "application/json;charset=UTF-8")
    String batchAddNoticeByWatch(@RequestBody NoticeDto noticeDto){
        return noticeService.batchAddNoticeByWatch(noticeDto);
    }

    /**
     * 批量保存 notice 数据
     * @param noticeList
     */
    @PostMapping(value = "/saveBatch", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = "application/json;charset=UTF-8")
    String saveNoticeBatch(@RequestBody List<NoticeDto> noticeList){
        List<NoticeEntity> noticeEntityList = noticeList.stream().map(item->{
            NoticeEntity noticeEntity = new NoticeEntity();
            BeanUtils.copyProperties(item, noticeEntity);
            return noticeEntity;
        }).collect(Collectors.toList());
        return ResultUtil.successWithData(noticeService.saveBatch(noticeEntityList));
    }
}
